package ${config.basePackage}.${artifact.relatedPackage};

import javax.validation.Valid;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.jaronsource.framework.plugins.easyui.EasyUIMessage;
import org.jaronsource.framework.plugins.easyui.EasyUIUtils;
import org.jaronsource.framework.plugins.report.DynamicReportBuilder;
import org.jaronsource.framework.util.BeanUtils;
import org.jaronsource.framework.util.JsonUtils;
import org.jaronsource.framework.util.NumberUtils;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import ${config.basePackage}.domain.${materialDetail.material.domainName};
import ${config.basePackage}.service.${materialDetail.material.domainName}Service;
import org.jaronsource.framework.core.dao.support.Page;
import org.jaronsource.framework.core.service.SearchForm;
import org.jaronsource.framework.core.web.controller.BaseController;
import org.jaronsource.framework.plugins.dictionary.DictionaryHelper;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/${materialDetail.material.domainName?uncap_first}")
@Controller
public class ${materialDetail.material.domainName}Controller extends BaseController {

	final Logger logger = LoggerFactory.getLogger(${materialDetail.material.domainName}Controller.class);	
	
	@Autowired
	private DictionaryHelper dictionaryHelper;
	
	@Autowired
	private ${materialDetail.material.domainName}Service ${materialDetail.material.domainName?uncap_first}Service;
	
	@RequestMapping(value="/list", method = {GET, POST})
	public String list(Model model) {
		return "${materialDetail.material.domainName?lower_case}/${materialDetail.material.domainName?lower_case}_list";
	}
	
	@RequestMapping(value="/ajaxList", method = {GET, POST})
	@ResponseBody
	public String ajaxList(@ModelAttribute SearchForm searchForm, Model model) {
		Page<${materialDetail.material.domainName}> ${materialDetail.material.domainName?uncap_first}Page = ${materialDetail.material.domainName?uncap_first}Service.findPage(searchForm);
		Page<Map<String, Object>> resultPage = dictionaryHelper.decodePage( ${materialDetail.material.domainName?uncap_first}Page );
		return EasyUIUtils.convertPageToJson( resultPage );
	}
	
	@RequestMapping(value = "/ajaxUpdate", method = GET)
	@ResponseBody
    public String ajaxUpdate(@RequestParam("pk") Integer pk, Model model) {
		try {
			${materialDetail.material.domainName} ${materialDetail.material.domainName?uncap_first} = ${materialDetail.material.domainName?uncap_first}Service.findByPk( pk );
			return JsonUtils.toJson( ${materialDetail.material.domainName?uncap_first} );
		}
		catch ( Exception e ) {
			String message = getMessage( "message.operation.find.error" );
			return JsonUtils.toJson( EasyUIMessage.fail( message ) );
		}
	}	
	
	@RequestMapping(value = "/ajaxUpdate", method = POST)
	@ResponseBody
    public String ajaxUpdate(@Valid @ModelAttribute ${materialDetail.material.domainName} ${materialDetail.material.domainName?uncap_first}, BindingResult bindingResult, Model model) {

        ${materialDetail.material.domainName?uncap_first}Service.update(${materialDetail.material.domainName?uncap_first}, ${materialDetail.material.domainName?uncap_first}.get${materialDetail.pk.propertyName?cap_first}());
        String message = getMessage( "message.operation.update.success" );
        return JsonUtils.toJson( EasyUIMessage.success( message ) );
    }	
	
	@RequestMapping(value = "/ajaxBatchUpdate", method = POST)
	@ResponseBody
    public String ajaxBatchUpdate(@RequestParam("pks") String pks, @RequestParam("field") String field, @RequestParam("fieldValue") String fieldValue, Model model) {
		
		Integer[] integerPks = NumberUtils.toIntegerArray( pks.split( "," ) );
		for ( Integer pk : integerPks ) {
			${materialDetail.material.domainName} ${materialDetail.material.domainName?uncap_first} = new ${materialDetail.material.domainName}();
			try {
				BeanUtils.copyProperty( ${materialDetail.material.domainName?uncap_first}, field, fieldValue );
			}
			catch ( Exception e ) {
			}
			${materialDetail.material.domainName?uncap_first}Service.update(${materialDetail.material.domainName?uncap_first}, pk);
		}
        
        String message = getMessage( "message.operation.update.success" );
        return JsonUtils.toJson( EasyUIMessage.success( message ) );
    }	
	
	@RequestMapping(value = "/ajaxCreate", method = GET)
	@ResponseBody
    public String ajaxCreate(Model model) {
        return JsonUtils.toJson( EasyUIMessage.success( null ) );
	}	
	
	@RequestMapping(value = "/ajaxCreate", method = POST)
	@ResponseBody
    public String ajaxCreate(@Valid @ModelAttribute ${materialDetail.material.domainName} ${materialDetail.material.domainName?uncap_first}, BindingResult bindingResult, Model model) {

		${materialDetail.material.domainName?uncap_first} = ${materialDetail.material.domainName?uncap_first}Service.save(${materialDetail.material.domainName?uncap_first});
		String message = getMessage( "message.operation.create.success" );
        return JsonUtils.toJson( EasyUIMessage.success( message ) );
    }	
	
	@RequestMapping(value = "/ajaxShow", method = GET)
	@ResponseBody
    public String ajaxShow(@RequestParam("pk") Integer pk, Model model) {
        ${materialDetail.material.domainName} ${materialDetail.material.domainName?uncap_first} = ${materialDetail.material.domainName?uncap_first}Service.findByPk(pk);
        return JsonUtils.toJson( ${materialDetail.material.domainName?uncap_first} );
    }
    
    @RequestMapping(value = "/ajaxRemove", method = POST)
    @ResponseBody
    public String ajaxRemove(@RequestParam("pks") String pks, Model model) {
    	
        try {
			${materialDetail.material.domainName?uncap_first}Service.remove(NumberUtils.toIntegerArray( pks.split( "," ) ));
			String message = getMessage( "message.operation.remove.success" );
			return JsonUtils.toJson( EasyUIMessage.success( message ) );
		}
		catch ( Exception e ) {
			String message = getMessage( "message.operation.remove.error" );
			return JsonUtils.toJson( EasyUIMessage.fail( message ) );
		}
    }
    
    @RequestMapping(value = "/report", method = {GET, POST})
    @ResponseBody
    public void report(@ModelAttribute SearchForm searchForm, HttpServletResponse response) {
		
    	List<${materialDetail.material.domainName}> ${materialDetail.material.domainName?uncap_first}List = ${materialDetail.material.domainName?uncap_first}Service.find( searchForm );
    	List<Map<String, Object>> convertedList = dictionaryHelper.decodeBeansToMaps(${materialDetail.material.domainName?uncap_first}List);
    	
		new DynamicReportBuilder()
<#list materialDetail.pcMappings as pcMapping>
<#if !pcMapping.isPk()> 
			//.addColumn("${pcMapping.columnRemarks!'未指定'}", "${pcMapping.propertyName}", ${pcMapping.propertyType}.class, 1)
</#if>
</#list>
			.outputExcel(response, convertedList, "${materialDetail.material.domainName}", "${materialDetail.material.domainName}");
    }

}

